<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>营销机会管理</title>
    <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/jquery-easyui-1.8.6/themes/default/easyui.css">
    <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/jquery-easyui-1.8.6/themes/icon.css">
    <script type="text/javascript" src="${pageContext.request.contextPath}/jquery-easyui-1.8.6/jquery.min.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/jquery-easyui-1.8.6/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/jquery-easyui-1.8.6/locale/easyui-lang-zh_CN.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/js/common.js"></script>
</head>
<body style="margin:1px">
<table id="dg" title="营销机会信息管理" class="easyui-datagrid" fitColumns="true" pagination="true" rownumbers="true"
       url="${pageContext.request.contextPath}/saleChance/list.do" fit="true" toolbar="#dg-tb">
    <thead>
    <tr>
        <th field="cb" checkbox="true" align="center"></th>
        <th field="id" width="50" align="center">编号</th>
        <th field="chanceSource" width="200" align="center" hidden="true">机会来源</th>
        <th field="customerName" width="80" align="center">客户名称</th>
        <th field="cgjl" width="50" align="center" hidden="true">成功几率</th>
        <th field="overView" width="200" align="center">概要</th>
        <th field="linkMan" width="100" align="center">联系人</th>
        <th field="linkPhone" width="100" align="center">联系电话</th>
        <th field="description" width="200" align="center" hidden="true">机会描述</th>
        <th field="createMan" width="100" align="center">创建人</th>
        <th field="createTime" width="100" align="center">创建时间</th>
        <th field="assignMan" width="200" align="center" hidden="true">指派人</th>
        <th field="assignTime" width="200" align="center" hidden="true">指派时间</th>
        <th field="state" width="100" align="center" formatter="formatState">状态</th>
        <th field="devResult" width="200" align="center" hidden="true">客户开发状态</th>
    </tr>
    </thead>
</table>
<div id="dg-tb">
    <div>
        <a href="javascript:openSaleChanceAddDialog()" class="easyui-linkbutton" iconCls="icon-add" plain="true">创建</a>
        <a href="javascript:openSaleChanceModifyDialog()" class="easyui-linkbutton" iconCls="icon-add" plain="true">修改</a>
        <a href="javascript:deleteSaleChance()" class="easyui-linkbutton" iconCls="icon-add" plain="true">删除</a>
    </div>
    <div>
        &nbsp;&nbsp;客户名称：<input class="easyui-textbox" id="s_customerName">
        &nbsp;&nbsp;概要：<input class="easyui-textbox" id="s_overView">
        &nbsp;&nbsp;创建人：<input class="easyui-textbox" id="s_createMan">
        &nbsp;&nbsp;状态：<select class="easyui-combobox" id="s_state" editable="false" panelHeight="auto" style="width:100px;">
        <option value="">请选择...</option>
        <option value="0">未分配</option>
        <option value="1">已分配</option>
    </select>
        <a href="javascript:searchSaleChance()" class="easyui-linkbutton" iconCls="icon-search" plain="true">搜索</a>
    </div>
</div>

<div id="dlg" class="easyui-dialog" style="width:700px;height:450px;padding: 10px 20px" closed="true" buttons="#dlg-buttons" data-options="onClose:closeSaleChanceDialog">
    <form id="fm" method="post">
        <table cellspacing="8px">
            <tr>
                <td>客户名称：</td>
                <td><input class="easyui-textbox" id="customerName" name="customerName" required="true"/>&nbsp;<font color="red">*</font></td>
                <td>&nbsp;&nbsp;&nbsp;&nbsp;</td>
                <td>机会来源：</td>
                <td><input class="easyui-textbox" id="chanceSource" name="chanceSource"/></td>
            </tr>
            <tr>
                <td>联系人：</td>
                <td><input class="easyui-textbox" id="linkMan" name="linkMan"/></td>
                <td>&nbsp;&nbsp;&nbsp;&nbsp;</td>
                <td>联系电话：</td>
                <td><input class="easyui-textbox" id="linkPhone" name="linkPhone"/></td>
            </tr>
            <tr>
                <td>成功几率(%)：</td>
                <td><input class="easyui-numberbox" id="cgjl" name="cgjl" data-options="min:0,max:100" required="true"/>&nbsp;<font color="red">*</font></td>
                <td colspan="3">&nbsp;&nbsp;&nbsp;&nbsp;</td>
            </tr>
            <tr>
                <td>概要：</td>
                <td colspan="4"><input class="easyui-textbox" id="overView" name="overView" multiline="true" style="width: 420px;height:44px"/></td>
            </tr>
            <tr>
                <td>机会描述：</td>
                <td colspan="4"><input class="easyui-textbox" id="description" name="description" multiline="true" style="width: 420px;height:44px"/></td>
            </tr>
            <tr>
                <td>创建人：</td>
                <td><input class="easyui-textbox" editable="false" id="createMan" name="createMan" required="true"/>&nbsp;<font color="red">*</font></td>
                <td>&nbsp;&nbsp;&nbsp;&nbsp;</td>
                <td>创建时间：</td>
                <td><input class="easyui-textbox" id="createTime" name="createTime" editable="false" required="true"/>&nbsp;<font color="red">*</font></td>
            </tr>
            <tr>
                <td>指派给客户经理：</td>
                <td><input class="easyui-combobox" id="assignMan" name="assignMan" data-options="panelHeight:'auto',editable:false,valueField:'trueName',textField:'trueName',url:'${pageContext.request.contextPath}/user/customerManagerComboList.do'"/></td>
                <td>&nbsp;&nbsp;&nbsp;&nbsp;</td>
                <td>指派时间：</td>
                <td><input class="easyui-textbox" id="assignTime" name="assignTime" editable="false"/></td>
            </tr>
        </table>
    </form>
</div>
<div id="dlg-buttons">
    <a href="javascript:saveSaleChance()" class="easyui-linkbutton" iconCls="icon-ok">保存</a>
    <a href="javascript:closeSaleChanceDialog()" class="easyui-linkbutton" iconCls="icon-cancel">关闭</a>
</div>

</body>
</html>
<script>
    $(function () {
        $('#s_customerName,#s_overView,#s_createMan').each(function (index,element){
            $(this).textbox('textbox').bind('keydown', function (e) {
                if (e.keyCode == 13) {   // 当按下回车键时接受输入的值。
                    searchCustomer();
                }
            });
        });

        $("#assignMan").combobox({
            onSelect: function (record) {
                if (record.trueName != '') {
                    $("#assignTime").textbox("setValue", getCurrentDateTime());
                } else {
                    $("#assignTime").textbox("setValue", "");
                }
            }
        });
    });

    var url = "";

    function formatState(val, row, index) {
        if (val == 1) {
            return "已分配";
        } else {
            return "未分配";
        }
    }

    function searchSaleChance() {
        $("#dg").datagrid("load", {
            "customerName": $("#s_customerName").textbox("getValue"),
            "overView": $("#s_overView").textbox("getValue"),
            "createMan": $("#s_createMan").textbox("getValue"),
            "state": $("#s_state").combobox("getValue")
        });
    }

    function openSaleChanceAddDialog() {
        $("#dlg").dialog("setTitle", "添加销售机会信息").dialog("open");
        $("#createMan").textbox("setValue",'${currentUser.trueName}');
        $("#createTime").textbox("setValue",getCurrentDateTime());
        url = "${pageContext.request.contextPath}/saleChance/save.do";
    }

    function openSaleChanceModifyDialog() {
        var selectedRows = $("#dg").datagrid("getSelections");
        if (selectedRows.length != 1) {
            $.messager.alert("系统提示", "请选择一条要编辑的数据！");
            return;
        }
        var selectedRow = selectedRows[0];
        $("#dlg").dialog("setTitle", "编辑销售机会信息").dialog("open");
        $("#fm").form("load", selectedRow);
        url = "${pageContext.request.contextPath}/saleChance/save.do?id=" + selectedRow.id;
    }

    function saveSaleChance() {
        $("#fm").form("submit", {
            url: url,
            onSubmit: function () {
                return $(this).form("validate");
            },
            success: function (result) {
                var result = eval('(' + result + ')');
                if(result.success){
                    $.messager.alert("系统提示","保存成功！");
                    $("#fm").form("reset");
                    $("#dlg").dialog("close");
                    $("#dg").datagrid("reload");
                }else{
                    $.messager.alert("系统提示","保存失败！");
                }
            }
        });
    }

    function closeSaleChanceDialog() {
        var dlgOptions = $("#dlg").dialog("options");
        if(dlgOptions.closed != true){
            $("#dlg").dialog("close");
        }
        $("#fm").form("reset");
        $("#dg").datagrid("reload");
    }

    function deleteSaleChance(){
        var selectedRows = $("#dg").datagrid("getSelections");
        if (selectedRows.length == 0) {
            $.messager.alert("系统提示", "请选择要删除的数据！");
            return;
        }
        var idsArr = [];
        for (var i = 0; i < selectedRows.length; i++) {
            idsArr.push(selectedRows[i].id);
        }
        var idsStr = idsArr.join(",");
        $.messager.confirm("系统提示", "您确定要删除这<span style='color: red'>" + selectedRows.length + "</span>条记录吗？", function (r) {
            if (r) {
                $.post("${pageContext.request.contextPath}/saleChance/delete.do", {ids: idsStr}, function (result) {
                    if (result.success) {
                        $.messager.alert("系统提示", "已成功删除数据！");
                        $("#dg").datagrid("reload");
                    } else {
                        $.messager.alert("系统提示", "删除数据失败，请联系系统管理员！");
                    }
                }, "json");
            }
        });
    }
</script>
